//获取应用实例
let app = getApp();
Page({
    data: {
        orderList: [],
        doingList: [],
		finishList: [],
		allList: [],
        doingState: false,
        finishState: false,
        allState: false,
        location: "doing"
	},
	
	onShow(options) {
		console.log(options)
	},

    onLoad(option) {
        this.queryOrderList(this,app.globalData.openId,option.bizFlag, (list)=> {
            this.setData({
                orderList: list
            })
        });
    },

    queryOrderList(pageObj,accountId,bizFlag) {
	    let location = "doing";
	    if (bizFlag==app.globalData.bizDoingFlag) {
		    location = "doing";
	    } else if(bizFlag==app.globalData.bizFinishFlag) {
		    location = "finish";
	    } else if(bizFlag==app.globalData.bizAllFlag) {
		    location = "all";
	    }
	    this.setData({
	      location: location
      })

      if (bizFlag==1) {
	    	/*
	    	*   doingState 防止数据为空时，点击会调用接口
	    	* */
        if (this.data.doingList.length||this.data.doingState) {
          this.setData({
            orderList: this.data.doingList
          })
        } else {
        this.getOrderList(pageObj,accountId,bizFlag,(list)=>{
	        pageObj.setData({
            orderList: list,
            doingList: list,
            doingState: true
          })
        })
        }
      } else if(bizFlag==2) {
	      if (this.data.finishList.length||this.data.finishState) {
		      this.setData({
			      orderList: this.data.finishList
		      })
	      } else {
		      this.getOrderList(pageObj,accountId,bizFlag,(list)=>{
			      pageObj.setData({
				      orderList: list,
				      finishList: list,
				      finishState: true
			      })
		      })
	      }
      } else if(bizFlag==3) {
	      if (this.data.allList.length||this.data.allState) {
		      this.setData({
			      orderList: this.data.allList
		      })
	      } else {
		      this.getOrderList(pageObj,accountId,bizFlag,(list)=>{
			      pageObj.setData({
				      orderList: list,
				      allList: list,
				      allState: true
			      })
		      })
	      }
      }
    },

	  getOrderList(pageObj,accountId,bizFlag,callBack) {
	    let opt = {
			url: app.globalData.host+"/wx/sd/list.ajax",
		    data: {
			    'accountId': accountId,
			    'bizFlag': bizFlag,
			    'orderType': app.globalData.orderType
		    },
		    sucFn:(result)=> {
			    callBack&&callBack(result.body.orderList)
		    }
	    };
	    app.ajax(opt);
    },

    doingTap(e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizDoingFlag);
    },
    finishTap(e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizFinishFlag);
    },
    allTap(e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizAllFlag);
    },
    orderDetailTap(e) {
        let orderId = e.currentTarget.dataset.id;
        app.navigateTo({
            url: '../orderdetail/orderdetail?orderId='+orderId
        });
    }
});
